package cate.game.pvp.eliterace.simulator;

import cate.game.framework.advanced.AdvancedByteAttributes;
import cate.game.framework.advanced.AdvancedLongAttributes;
import cate.game.pvp.eliterace.data.EliteRaceKV;
import cate.game.pvp.eliterace.data.EliteRaceSpec;
import cate.game.pvp.eliterace.handler.*;
import cate.game.pvp.eliterace.timer.EliteRaceTimerTask.Settings;

import java.util.function.Supplier;

import static cate.game.pvp.eliterace.data.EliteRaceKV.OP_SKIP_SEASON;
import static cate.game.pvp.eliterace.util.EliteRaceSpecUtils.*;

public class BothEliteRaceBetRwdSimulator extends EliteRaceSimulator {

    public BothEliteRaceBetRwdSimulator(Supplier<EliteRaceKV> kvSupplier,
                                        Supplier<EliteRaceRecordOperation> recordOperationSupplier,
                                        Supplier<EliteRacePsOperation> psOperationSupplier,
                                        Supplier<EliteRaceFightOperation> fightOperationSupplier,
                                        Supplier<EliteRaceRankRwdOperation> rankRwdOperationSupplier,
                                        Supplier<EliteRaceBetOperation> betOperationSupplier,
                                        Supplier<EliteRaceVideoOperation> videoOperationSupplier) {
        super(kvSupplier,
                recordOperationSupplier,
                psOperationSupplier,
                fightOperationSupplier,
                rankRwdOperationSupplier,
                betOperationSupplier,
                videoOperationSupplier
        );
    }

    @Override
    public boolean isMatch(Settings settings) {
        return isMatchBetRwdSimulator(settings);
    }


    @Override
    public boolean runWith(Settings settings) {
        runPeriodWith(settings);
        return true;
    }

    private int getPeriodFlag(Settings settings) {
        switch (settings.getEventType()) {
            case TRIAL:
                return getTrialFlag(settings);
            case KO:
                return getKoFlag(settings);
            case FINAL:
                return getFinalFlag(settings);
            default:
                return -1;
        }
    }

    private void runPeriodWith(Settings settings) {
        int flag = getPeriodFlag(settings);
        if (flag == -1) {
            return ;
        }
        AdvancedByteAttributes opAttributes = getOpAttributes();
        AdvancedLongAttributes attributes = getAttributes();
        if (opAttributes.hasAttribute(OP_SKIP_SEASON)) {
            getKV().setSpec(settings.getSpec());
            attributes.setAttribute(flag);
            return ;
        }
        if (attributes.hasAttribute(flag)) {
            return ;
        }
        EliteRaceKV kv = getKV();
        kv.setSpec(settings.getSpec());
        handleVideo(settings.getSpec());
        attributes.setAttribute(flag);
    }

    private void handleVideo(EliteRaceSpec spec) {
        EliteRaceVideoOperation videoOperation = getVideoOperation();
        videoOperation.handle(EliteRaceSpec.of(
                spec.getSeason(),
                spec.getPeriod(),
                spec.getGroup(),
                spec.getRound(),
                (byte) 0
        ));
    }
}